<template>
	<view class="page">
		<uni-nav-bar leftWidth="40rpx" leftIcon="back" @clickLeft="back" background-color="#b184fe" color="#ffffff" :border="false" :fixed="true">
			<view class="nav-scroll">
				<view class="nav uni-nav">
					<view class="nav-item tab" :class="swiperIndex == 0 ? 'nav-item-act' : ''" @click="taggleNav(0)">收益榜
					</view>
					<view class="nav-item tab" :class="swiperIndex == 1 ? 'nav-item-act' : ''" @click="taggleNav(1)">贡献榜
					</view>
					<view class="nav-line" :style="style"></view>
				</view>
			</view>
		</uni-nav-bar>
		
		<view class="swiper">
			<swiper :disable-touch="touch" :current="swiperIndex" :duration="300" class="swiper-1" easing-function="linear"
				@change="swiperChange" :style="{ height: swiperheight + 'px' }">
				<!-- @change="swiperChange" > -->
				<!-- 收益榜 -->
				<swiper-item @touchmove="touchmove" @touchstart="touchstart">
					<scroll-view scroll-y="true" class="swiper-scroll">
						<view class="head">
							<view class="pill">
								<view class="item" :class="type1 == 'day' ? 'active' : ''" @click="opentype1('day')">日榜</view>
								<view class="item" :class="type1 == 'yesterday' ? 'active' : ''" @click="opentype1('yesterday')">昨日榜</view>
							</view>
						</view>
						<view class="geban">隔板</view>
						<view class="user" v-for="(item, index) in list1" :key="index">
							<view class="left">
								<view class="one">NO.{{index + 1}}</view>
								<view class="two">
									<image :src="item.avatar" mode=""></image>
								</view>
								<view class="three">{{item.user_nicename}}</view>
							</view>
							<view class="right">{{item.totalcoin}}金币</view>
						</view>
					</scroll-view>
				</swiper-item>
				<!-- 贡献榜 -->
				<swiper-item @touchmove="touchmove" @touchstart="touchstart">
					<scroll-view scroll-y="true" class="swiper-scroll">
						<view class="head">
							<view class="pill">
								<view class="item" :class="type2 == 'day' ? 'active' : ''" @click="opentype2('day')">日榜</view>
								<view class="item" :class="type2 == 'yesterday' ? 'active' : ''" @click="opentype2('yesterday')">昨日榜</view>
							</view>
						</view>
						<view class="geban">隔板</view>
						<view class="user" v-for="(item, index) in list2" :key="index">
							<view class="left">
								<view class="one">NO.{{index + 1}}</view>
								<view class="two">
									<image :src="item.avatar" mode=""></image>
								</view>
								<view class="three">{{item.user_nicename}}</view>
							</view>
							<view class="right">{{item.totalcoin}}金币</view>
						</view>
					</scroll-view>
				</swiper-item>

			</swiper>

		</view>

	</view>
</template>

<script>
	export default {
		components: {
		},
		data() {
			return {
				swiperIndex: 0, //当前的swiperindex
				touch:false, //是否允许滑动
				touchStart:0,//第一次滑动的位置
				navItemWidth: '', //选中下划线的宽度
				navItemLeft: 0, //选中下划线的显示位置
				scrollToLeft: 0, //scrollview需要滚动的距离
				navInfos: [], //所有navitem的节点信息
				parentLeft: 0, //nav盒子的节点信息
				componentWidth: 0, //nav盒子的宽度
				refreStatus: false,
				selectedPaymentMethod: '',
				type1: 'day',
				type2: 'day',
				swiperh: '100vh',
				swiperheight: '100vh',//削去高度
				height: '190upx',//削去高度
				list1: [],
				list2: [],
			};
		},
		onLoad() {
			this.profitList();
			this.consumeList();
		},
		onUnload() {
			
		},
		computed: {
			style() {
				return `width:${this.navItemWidth}px; left:${this.navItemLeft}px`;
			}
		},
		mounted() {
			this.init();
		},
		methods: {
			// 收益榜
			profitList(){
				var token = '';
				var uid = 0;
				if(uni.getStorageSync('token')){
					token = uni.getStorageSync('token');
					uid = uni.getStorageSync('uid');
				}
				this.$api.sendRequest({
					data:{
						service:'Home.profitList',
						uid:uid,
						token:token,
						type:this.type1,
						p:1,
					},
					success: res => {
						if(res.data.code == 0){
							var param = res.data.info;
							this.list1 = param;
						}
					}
				});
			},
			// 贡献榜
			consumeList(){
				var token = '';
				var uid = 0;
				if(uni.getStorageSync('token')){
					token = uni.getStorageSync('token');
					uid = uni.getStorageSync('uid');
				}
				this.$api.sendRequest({
					data:{
						service:'Home.consumeList',
						uid:uid,
						token:token,
						type:this.type2,
						p:1,
					},
					success: res => {
						if(res.data.code == 0){
							var param = res.data.info;
							this.list2 = param;
						}
					}
				});
			},
			touchstart(res){
				this.touchStart = res.touches[0].clientX;
			},
			touchmove(res){
				if(this.swiperIndex == 0){
					res.touches[0].clientX> this.touchStart ? this.touch = true : this.touch = false;
					//如果滑动到了第一项，就禁止再向左滑动
				}else if(this.swiperIndex == 1){
					res.touches[0].clientX< this.touchStart ? this.touch = true : this.touch = false;
					//如果滑动到了最后一项，就禁止向右滑动
				}else{
					//以上条件都不符合，就随便滑动
					this.touch = false
				}
			},
			back() {
				uni.switchTab({
					url: "/pages/index/index",
				})
			},
			// 获取dom信息
			init() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.uni-nav').fields({
					rect: true,
					size: true
				}, res => {
					this.parentLeft = res.left;
					this.componentWidth = res.width;
					// var height = res.height + res.bottom;
					this.height = res.height;
					// console.log(res)
				});
				query.select('.swiper').fields({
					rect: true,
					size: true
				}, res => {
					this.swiperheight = res.height - this.height - 20;
					this.swiperh = res.height;
				});
				query.selectAll('.nav-item').fields({
					rect: true,
					size: true
				}, data => {
					data.forEach((item, index) => {
						if (index == 0) {
							this.navItemWidth = item.width;
							this.navItemLeft = item.left;
						}
						this.navInfos.push({
							width: item.width,
							left: item.left
						});
					});
				});
				query.exec();
			},
			// 点击导航切换swiper
			taggleNav(val) {
				this.swiperIndex = val;
			},
			opentype1(val) {
				this.type1 = val;
				this.profitList();
			},
			opentype2(val) {
				this.type2 = val;
				this.consumeList();
			},
			// 滚动tabs以及移动下划线
			scrollDom() {
				let info = this.navInfos[this.swiperIndex];
				let offsetLeft = info.left - this.parentLeft;
				let scrollLeft = offsetLeft - (this.componentWidth - info.width) / 2;
				this.scrollToLeft = scrollLeft < 0 ? 0 : scrollLeft;
				this.navItemLeft = this.navInfos[this.swiperIndex].left;
				setTimeout(() => {
					this.navItemWidth = info.width;
				}, 50);
			},
			radioChange(e) {
				this.selectedPaymentMethod = e.detail.value;
			},
			// swiper的index变动
			swiperChange(e) {
				this.swiperIndex = e.detail.current;
				this.scrollDom();
				// this.$emit('currentIndex', this.swiperIndex);
			},
		}
	};
</script>

<style lang="scss">
	/deep/ .uniui-back{
		font-size: 48rpx !important; 
	}
	page,
	.page {
		width: 750rpx;
		background-color: #ffffff;
		height: 100%;
	}
	.head{
		position: fixed;
		z-index: 999;
		width: 750rpx;
		height: 490rpx;
		background-image: url('@/static/image/rankbg.png');
		background-size: cover;
		background-repeat: no-repeat;
		background-position: center center;
		.pill{
			width: 280rpx;
			height: 60rpx;
			margin: 50rpx auto;
			background-color: #ffffff7a;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-radius: 50rpx;
			padding: 10rpx;
			.active{
				// width: 100rpx;
				
				background-color: #ffffff;
				color: #30B0FC !important;
			}
			.item{
				font-size: 28rpx;
				height: 60rpx;
				line-height: 60rpx;
				// width: 200rpx;
				padding: 0rpx 30rpx;
				border-radius: 50rpx;
				color: #ffffff;
			}
		}
	}
	.geban {
		height: 490rpx;
		color: #ffffff;
	}

	
	.nav-scroll {
		width: 100%;
		display: flex;
		justify-content: flex-start;
		// background: #ffffff;
		// height: 100rpx;
		// z-index: 999;
		// position: relative;
		// position: fixed;
		// top: 80rpx; 
	}
	.nav {
		white-space: nowrap;
		// position: fixed;
		// position: relative;
		// height: 60upx;
		// z-index: 999;

		// display: flex;
		// justify-content: space-between;
		// align-items: center;
		padding: 20rpx 20rpx;
		
	}
	.nav-item {
		display: inline-block;
		margin: 0 16upx;
		text-align: center;
		transition: color 0.3s ease;
	}
	/* #ifndef APP-NVUE */
	::-webkit-scrollbar {
		display: none;
	}

	/* #endif */

	/* #ifdef H5 */
	// 通过样式穿透，隐藏H5下，scroll-view下的滚动条
	scroll-view ::v-deep ::-webkit-scrollbar {
		display: none;
	}

	/* #endif */
	.tab {
		font-size: 40rpx;
		color: #ffffff;
	}

	.nav-item-act {
		color: #ffffff;
		font-size: 40rpx;
		font-weight: bold;
	}

	.nav-line {
		position: absolute;
		bottom: 0;
		height: 6upx;
		border-radius: 10upx;
		background-color: #ffffff;
		transition: left 0.3s ease;
	}

	swiper {
		// height: 100vh;
		// overflow: hidden;
		height: calc(100vh - 88rpx);
		// height: calc(100vh - 190upx);
	}

	.swiper {

		// position: relative;
		// top: 120rpx;

		// margin-top: 100rpx;
	}

	swiper-item {
		position: relative;
		color: #000;
	}

	.swiper-item {
		overflow-y: scroll;
		// height: 100%;
	}
</style>
<style lang="scss">
.swiper-scroll {
	height: 100%;
	.user{
		width: 710rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx;
		.left{
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 90rpx;
			view{
				margin-right: 20rpx;
			}
			.one{
				color: #999999;
				font-size: 24rpx;
			}
			.two{
				width: 90rpx;
				height: 90rpx;
				image{
					width: 100%;
					height: 100%;
					border-radius: 60rpx;
				}
			}
			.three{
				color: #1D1D1D;
				font-size: 30rpx;
			}
		}
		.right{
			color: #999999;
			font-size: 28rpx;
		}
	}
}
</style>